package com.meter.data.controller;

import java.util.List;

import com.meter.basic.domain.BasicDevice;
import com.meter.basic.service.IBasicDeviceService;
import com.meter.common.core.text.Convert;
import com.meter.common.utils.ShiroUtils;
import com.meter.common.utils.StringUtils;
import com.meter.common.utils.poi.ExcelUtil;
import com.meter.data.domain.MeterData;
import com.meter.redis.service.RedisService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.meter.common.annotation.Log;
import com.meter.common.enums.BusinessType;
import com.meter.data.service.IMeterDataService;
import com.meter.common.core.controller.BaseController;
import com.meter.common.core.domain.AjaxResult;
import com.meter.common.core.page.TableDataInfo;

/**
 * 热采集数据Controller
 * 
 * @author ruoyi
 * @date 2024-10-05
 */
@Controller
@RequestMapping("/data/meter")
public class MeterDataController extends BaseController
{
    private String prefix = "data/meter";

    @Autowired
    private IMeterDataService meterDataService;

    @Autowired
    private IBasicDeviceService basicDeviceService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("data:meter:view")
    @GetMapping()
    public String meter()
    {
        return prefix + "/meter";
    }

    /**
     * 查询热采集数据列表
     */
    @RequiresPermissions("data:meter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeterData meterData)
    {
        List<MeterData> list = meterDataService.selectMeterDataList(meterData);
        return getDataTable(list);
    }

    /**
     * 导出热采集数据列表
     */
    @RequiresPermissions("data:meter:export")
    @Log(title = "热采集数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MeterData meterData)
    {
        List<MeterData> list = meterDataService.selectMeterDataList(meterData);
        ExcelUtil<MeterData> util = new ExcelUtil<MeterData>(MeterData.class);
        return util.exportExcel(list, "热表采集数据");
    }

    /**
     * 读取热表数据
     */
    @Log(title = "读取热表数据", businessType = BusinessType.OTHER)
    @RequiresPermissions("basic:meter:reading")
    @PostMapping("/reading")
    @ResponseBody
    public AjaxResult reading(String meterIds) {
        String message = meterDataService.readingMeterData(meterIds, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 读取热表数据
     */
    @Log(title = "读取热表数据", businessType = BusinessType.OTHER)
    @RequiresPermissions("basic:device:reading")
    @PostMapping("/readingDevice")
    @ResponseBody
    public AjaxResult readingDevice(String deviceIds) {
        StringBuilder successMsg = new StringBuilder();
        for(Long deviceId : Convert.toLongArray(deviceIds)) {
            BasicDevice basicDevice = basicDeviceService.getById(deviceId);
            if(redisService.hHasKey("sys-netty", basicDevice.getDeviceCode())) {
                meterDataService.readingMeterDataByDevice(deviceId, basicDevice.getDeviceCode(), ShiroUtils.getLoginName());
                successMsg.append("<br/>" + " 采集设备 " + basicDevice.getDeviceCode() + " 发送抄表命令成功");
            } else {
                successMsg.append("<br/>" + " 采集设备 " + basicDevice.getDeviceCode() + " 不在线，发送抄表命令失败");
            }
        }

        return AjaxResult.success(successMsg.toString());
    }
}
